package com.voice.client.auth.handle;

import com.voice.client.auth.AuthHandler;
import org.apache.commons.codec.digest.HmacAlgorithms;
import org.apache.commons.codec.digest.HmacUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

public class AppKeyAuthHandler implements AuthHandler {


    private static final String SHA_256 = "HMAC_SHA_256";
    private static final String PARAM_SIGN = "param-sign";
    private static final String PARAM_TIMESTAMP = "param-timestamp";
    private static final String PARAM_SIGN_METHOD = "param-sign-method";
    private Map<String, Object> params = new HashMap<>();

    public AppKeyAuthHandler(Map<String, String> authParams) {
        this.params.putAll(authParams);
        long now = System.currentTimeMillis();
        String appSecret =authParams.get("appSecret");
        String signContent = "appSecret" + appSecret +"timestamp" + now;
        String sign = (new HmacUtils(HmacAlgorithms.HMAC_SHA_256, appSecret)).hmacHex(signContent);
        this.params.put(PARAM_SIGN, sign);
        this.params.put(PARAM_TIMESTAMP, now);
        this.params.put(PARAM_SIGN_METHOD, SHA_256);
        this.params.remove("appSecret");
    }

    public Map<String, Object> getAuthParams() {
        return this.params;
    }
}
